package com.sf.sms.software.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sf.sms.software.dao.SoftwareMapper;
import com.sf.sms.software.entity.Software;
import com.sf.sms.software.entity.dto.SoftwareDTO;
import com.sf.sms.software.service.SoftwareService;
import com.sf.sms.utils.SmsContants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 软件操作：实现类
 * Created by zhangbo 01084074 on 2016/8/25.
 */
@Service
public class SoftwareServiceImpl implements SoftwareService {

    @Autowired
    SoftwareMapper softwareMapper;

    @Override
    public int createSoftware(Software software) {
        return softwareMapper.addSoftware(software);
    }

    @Override
    public String reviewSoftwareSuccess(Long softwareId) {
        return null;
    }

    @Override
    public List<SoftwareDTO> getSoftwareByCategoryId(Long categoryId, Integer pageNum) {
        PageHelper.startPage(pageNum, SmsContants.PAGE_SIZE, true);
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getSoftwareByCategory(categoryId));
        List<Software> softwareList = pageInfo.getList();

        return softwareToDTO(softwareList);
    }

    @Override
    public List<SoftwareDTO> getAllSoftware(Integer pageNum) {
        PageHelper.startPage(pageNum, SmsContants.PAGE_SIZE, true);
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getAllSoftware());
        List<Software> softwareList = pageInfo.getList();
        return softwareToDTO(softwareList);
    }

    @Override
    public List<SoftwareDTO> getReviewSoftware(Integer pageNum) {
        PageHelper.startPage(pageNum, SmsContants.PAGE_SIZE, true);
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getSoftwareByStatus(SmsContants.SOFTWARE_STATUS_REVIEW));
        List<Software> softwareList = pageInfo.getList();
        return softwareToDTO(softwareList);
    }

    @Override
    public List<SoftwareDTO> getReviewSoftwareByCategory(Long categoryId, Integer pageNum) {
        PageHelper.startPage(pageNum, SmsContants.PAGE_SIZE, true);
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getReviewSoftwareByCategory(categoryId));
        List<Software> softwareList = pageInfo.getList();
        return softwareToDTO(softwareList);
    }

    @Override
    public Long getAllSoftwareCount() {
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getAllSoftware());
        return pageInfo.getTotal();
    }

    @Override
    public Long getSoftwareCountByCategoryId(Long categoryId) {
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getSoftwareByCategory(categoryId));
        return pageInfo.getTotal();
    }

    @Override
    public List<SoftwareDTO> searchSoftware(String content, Integer pageNum) {
        PageHelper.startPage(pageNum, SmsContants.PAGE_SIZE, true);
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.searchSoftware(content));
        List<Software> softwareList = pageInfo.getList();
        return softwareToDTO(softwareList);
    }

    @Override
    public Long searchSoftwareCount(String content) {
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.searchSoftware(content));
        return pageInfo.getTotal();
    }

    @Override
    public Long getReviewSoftwareCount() {
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getSoftwareByStatus(SmsContants.SOFTWARE_STATUS_REVIEW));
        return pageInfo.getTotal();
    }

    @Override
    public Long getReviewSoftwareByCategoryCount(Long categoryId) {
        PageInfo<Software> pageInfo = new PageInfo<Software>(softwareMapper.getReviewSoftwareByCategory(categoryId));
        return pageInfo.getTotal();
    }

    private List<SoftwareDTO> softwareToDTO(List<Software> list) {
        List<SoftwareDTO> result = new ArrayList<SoftwareDTO>();

        for ( Software s : list ) {
            SoftwareDTO dto = new SoftwareDTO();
            dto.setId(s.getId());
            dto.setName(s.getName());
            dto.setDescription(s.getDescription());
            dto.setDownloadUrl(s.getDownloadUrl());
            dto.setIcon(s.getIcon());
            dto.setSize(s.getSize());
            DateFormatter dateFormatter = new DateFormatter();
            dateFormatter.setPattern("yyyy-MM-dd");
            String updateTime = dateFormatter.print(s.getModifyTime(), Locale.CHINA);
            dto.setUpdateTime(updateTime);
            result.add(dto);
        }

        return result;
    }

    @Override
    public Software getSoftwareById(Long id) {
        return softwareMapper.getSoftwareById(id);
    }
}
